<template>
  <div class="home-container">
    <!-- 顶部导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <template #title>
        <van-button
          class="search-btn"
          type="info"
          round
          size="small"
          icon="search"
          to="/search"
        >
          搜索
        </van-button>
      </template>
    </van-nav-bar>
    <!-- 顶部导航栏 -->

    <!-- 频道列表 -->
    <!-- active 绑定当前激活标签对应的索引值 -->
    <van-tabs
      class="channel-tabs"
      v-model="active.index"
      animated
      swipeable
      swipe-threshold="4"
    >
      <van-tab :title="item.name" v-for="item in channelList" :key="item.id">
        <!-- 文章列表 -->
        <article-list :channel="item"></article-list>
        <!-- 文章列表 -->
      </van-tab>
      <div class="placeholder" slot="nav-right"></div>
      <div
        slot="nav-right"
        class="hamburger-btn"
        @click="active.isChannelEditShow = true"
      >
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <!-- 频道列表 -->

    <!-- 修改用户频道弹出层 -->
    <van-popup
      lazy-render
      v-model="active.isChannelEditShow"
      closeable
      position="bottom"
      :style="{ height: '100%' }"
      close-icon-position="top-left"
    >
      <!-- 弹出层内容组件 -->
      <ChannelEdit :myChannels="channelList" v-model="active" />
    </van-popup>
    <!-- 修改用户频道弹出层 -->
  </div>
</template>

<script>
import ArticleList from './components/ArticleList.vue'
import ChannelEdit from './components/ChannelEdit.vue'
import { getItem } from '@/utils/storage'
export default {
  name: 'HomeIndex',
  components: {
    ArticleList,
    ChannelEdit
  },
  data() {
    return {
      active: {
        index: 0,
        isChannelEditShow: false // 控制频道编辑弹出层的展示和隐藏
      }, // 默认第一个标签页被激活
      channelList: [] // 频道列表
    }
  },
  created() {
    // 获取频道列表
    this.loadChannels()
  },
  methods: {
    // 获取频道列表
    async loadChannels() {
      try {
        let channels = []
        if (this.$store.state.user) {
          // 已登录
          const { data: res } = await this.$api.getUserChannel()
          channels = res.data.channels
        } else {
          // 未登录
          const localChannels = getItem('TOUTIAO_CHANNELS')
          console.log(localChannels)
          if (localChannels) {
            channels = localChannels
          } else {
            const { data: res } = await this.$api.getUserChannel()
            channels = res.data.channels
          }
        }
        this.channelList = channels
      } catch (err) {
        this.$toast('获取频道列表失败')
      }
    }
    // #region
    // onUpdateActive(index, isChannelEditShow = true) {
    //   // 切换频道
    //   this.active = index
    //   // 关闭编辑频道弹层
    //   this.isChannelEditShow = isChannelEditShow
    // }
    // #endregion
  }
}
</script>

<style scoped lang="less">
.home-container {
  padding-top: 174px;
  padding-bottom: 100px;
  /deep/ .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;

    .van-icon {
      font-size: 32px;
    }
  }

  /deep/ .channel-tabs {
    .van-tabs__wrap {
      height: 82px;
      position: fixed;
      top: 92px;
      left: 0;
      z-index: 1;
      right: 0;
      border-bottom: 1px solid #ccc;
    }

    .van-tab {
      border-right: 1px solid #edeff3;
      min-width: 200px;
      font-size: 30px;
      color: #777777;
    }

    .van-tab--active {
      color: #333333;
    }

    .van-tabs__nav--line {
      padding-bottom: 0;
    }

    .van-tabs__line {
      width: 31px;
      height: 6px;
      background-color: #3296fa;
      bottom: 8px;
    }
  }
  .placeholder {
    width: 50px;
    height: 82px;
    flex-shrink: 0;
  }
  .hamburger-btn {
    font-weight: 700;
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 82px;
    background-color: #fff;
    opacity: 0.902;
    z-index: 999999;
    i.toutiao {
      font-size: 33px;
    }
    &::before {
      content: '';
      position: absolute;
      left: 0;
      width: 1px;
      height: 100%;
      background: url('~@/assets/gradient-gray-line.png');
    }
  }
}
</style>
